package com.huijinhui.wlmtsys.controller;

import com.huijinhui.wlmtsys.constant.JwtClaimsConstant;
import com.huijinhui.wlmtsys.enums.AppHttpCodeEnum;
import com.huijinhui.wlmtsys.pojo.dtos.AdUserDto;
import com.huijinhui.wlmtsys.pojo.dtos.PageDTO;
import com.huijinhui.wlmtsys.pojo.dtos.ResponseResult;
import com.huijinhui.wlmtsys.pojo.entity.AdUser;
import com.huijinhui.wlmtsys.pojo.entity.PageQuery;
import com.huijinhui.wlmtsys.pojo.entity.WlmtsysInfo;
import com.huijinhui.wlmtsys.pojo.vo.AdminLoginVO;
import com.huijinhui.wlmtsys.properties.JwtProperties;
import com.huijinhui.wlmtsys.service.AdminService;
import com.huijinhui.wlmtsys.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequiredArgsConstructor
@Tag(name = "admin----管理员相关接口")
@RequestMapping("/admin")
public class AdminController {

    final AdminService adminService;
    final JwtProperties jwtProperties;

    @GetMapping("/queryAll")
    @Operation(summary = "admin---查询所有统计·接口")
    public ResponseResult queryAll() {
        return adminService.queryAll();
    }

    @Operation(summary = "admin---管理员登录·接口")
    @PostMapping("/login")
    public ResponseResult login(@RequestBody AdUserDto adUserDto) {
        log.info("login request received, adUserDto={}", adUserDto);
        AdUser admin = adminService.login(adUserDto);
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, admin.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        AdminLoginVO employeeLoginVO = AdminLoginVO.builder()
                .id(Long.valueOf(admin.getId()))
                .name(admin.getName())
                .token(token)
                .build();

        return ResponseResult.okResult(employeeLoginVO);
    }

    @Operation(summary = "admin+官网---实验室简介查询接口")
    @GetMapping("/info")
    public ResponseResult getLabIntro() {
        log.info("实验室简介接口");
        return adminService.getLabIntro();
    }
    @Operation(summary = "admin---图片接口1")
    @PostMapping("/upload1")
    public ResponseResult upload1(@RequestParam("file") MultipartFile multipartFile, @RequestParam("id") Integer imageId) {
        log.info("图片接口,{},id:{}", multipartFile, imageId);
        adminService.upload1(multipartFile, imageId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    @Operation(summary = "admin---图片接口1")
    @PostMapping("/upload2")
    public ResponseResult upload2(@RequestParam("file") MultipartFile multipartFile, @RequestParam("id") Integer imageId) {
        log.info("图片接口,{},id:{}", multipartFile, imageId);
        adminService.upload2(multipartFile, imageId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Operation(summary = "admin---图片接口1")
    @PostMapping("/upload3")
    public ResponseResult upload3(@RequestParam("file") MultipartFile multipartFile, @RequestParam("id") Integer imageId) {
        log.info("图片接口,{},id:{}", multipartFile, imageId);
        adminService.upload3(multipartFile, imageId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Operation(summary = "admin---图片接口1")
    @PostMapping("/upload4")
    public ResponseResult upload4(@RequestParam("file") MultipartFile multipartFile, @RequestParam("id") Integer imageId) {
        log.info("图片接口,{},id:{}", multipartFile, imageId);
        adminService.upload4(multipartFile, imageId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Operation(summary = "admin---分页 获取管理员接口")
    @GetMapping("/page")
    public  List<AdUser>  queryNewsPage(){
        log.info("分页 获取信息接口");
        return adminService.queryEmployPage();
    }

    @Operation(summary = "admin---修改密码接口")
    @PutMapping("/update")
    public void updateLabInfo(@RequestBody AdUser adUser) {
        log.info("修改实验室简介接口");
        adminService.updateLabInfo(adUser);
    }

}
